import { http } from "../../utils/http"
import differenceBy from "lodash/differenceBy"
// 获取我的频道
export const getChannels = () => async (dispatch) => {
  const { data } = await http.get("/user/channels")
  // 将我的频道保存到本地内存中
  localStorage.setItem("my_channel", JSON.stringify(data.data.channels))
}
// 获取可选频道
export const getAllChannels = () => async (dispatch, getState) => {
  // 全部频道
  const { data } = await http.get("/channels")
  const channels = JSON.parse(localStorage.getItem("my_channel"))
  // 计算可选频道
  // 通过 differenceBy 方法，从 第一个参数 中 排除掉 第二个参数的数据
  const restChannels = differenceBy(data.data.channels, channels, "id")
  // 保存可选频道列表
  dispatch({ type: "GET_CHANNEL", payload: restChannels })
}
// 更改可选频道
// export const setAllChannels = (paylaod) => async (dispatch) => {
//   dispatch({ type: "GET_CHANNEL", payload: restChannels })
// }
//  获取推荐新闻
export const getArticleList = (id, timestamp) => async (dispatch) => {
  // 获取新闻列表
  // 获取当前时间戳
  // const time = Date.now() + ""
  // const time = 1653295639771
  console.log(timestamp)
  const { data } = await http.get("/articles", {
    params: {
      channel_id: id,
      timestamp: timestamp || Date.now()
    }
  })
  dispatch({ type: "GET_ARTICLES", payload: data.data })
}
// 切换tab标签
export const toggleTab = (key) => async (dispatch) => {
  dispatch({ type: "TOGGLE_TAB", payload: key })
}
